<?php
/**
 * Created by PhpStorm.
 * User: Kiriti
 * Date: 2016/4/1 0001
 * Time: 14:57
 */

namespace app\controllers;

use yii\web\Controller;

use Yii;
use app\models\Uc_msg;

use yii\data\Pagination;
use app\models\Uc_user;

class MymsgController extends Controller{
	
    public function actionShow(){


        if(!empty($_COOKIE['user_id'])){
            $user_id = $_COOKIE['user_id'];

            //投资数量 关注数量 发起数量
            $user = Uc_user::find()->where([
                'user_id' => $user_id,
                'del_flag' => 0
                ])->one();

            $view = Yii::$app->view;
            $view->params['title'] = "我的消息";
            PrlikeController::setView($user);


//            $query = Uc_msg::find()
//            ->where([
//                'to_user' => $user_id, //user_id => user_id
//                'del_flag' => 0
//            ])->orderBy('msg_id desc')->limit(10)->all();


            $query = Uc_msg::find()
            ->where([
                'to_user' => $user_id, //user_id => user_id
                'del_flag' => 0
            ]);

            $pagination = new Pagination([
                'defaultPageSize' => 10,
                'totalCount' => $query->count()
            ]);
            $msgList = $query->orderBy('msg_id DESC')
                ->offset($pagination->offset)
                ->limit($pagination->limit)
                ->all();






            $count = Uc_msg::find()
            ->where([
                'to_user' => $user_id, //user_id => user_id
                'del_flag' => 0,
                'is_read' =>0
                ])->count();
            $unread = Uc_msg::find()
            ->where([
                'to_user' => $user_id, //user_id => user_id
                'del_flag' => 0,
                'is_read' =>0
                ])->limit(10)->all();
            return $this->render('mymsg', ['msgList'=>$msgList, 'unreadCount'=>$count, 'unread'=>$unread,
                'user'=>$user, 'pages'=>$pagination]);

        }else{
            echo "<script>alert('cookie 不存在')</script>";
            // sleep(3);
            // $this->redirect(['login/show']);
        }

       		
	}

    public function logout(){
        if(!empty($_COOKIE['user_id'])){
            setcookie("user_id",time()-10);
            return $this->redirect(['index/show']);

        }else{
            echo "<script>alert('cookie 不存在')</script>";
            sleep(3);
            $this->redirect(['login/show']);
        }
    }



}